#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

ONE_LOCATION=ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
    ETC_LOCATION="/etc/one/"
    VAR_LOCATION="/var/lib/one"
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
    ETC_LOCATION=ONE_LOCATION+"/etc/"
    VAR_LOCATION="#{ONE_LOCATION}/var"
end

$: << RUBY_LIB_LOCATION


require 'OpenNebula'

require 'rubygems'
require 'sequel'
require 'quota'
require 'ssh_auth'
require 'client_utilities'
require 'command_parse'
require 'yaml'


COMMANDS_HELP=<<-EOT
Usage:
    oneauth <command> [<parameters>]


Description:

This command contains a set of utilities to manage authorization module.


Commands:

* quota set (sets quota for a user)
    oneauth quota set <id> <cpu> <memory> <num_vms>
    
* login (generates authentication proxy)
    oneauth login <username> [<expire time in seconds>]
    
* key (gets public key)
    oneauth key
    
* help (prints help)
    oneauth help

EOT

def print_help
    puts COMMANDS_HELP
end

def get_database
    config_data=File.read(ETC_LOCATION+'/auth/auth.conf')
    config=YAML::load(config_data)
    
    database_url=config[:database]
    db=Sequel.connect(database_url)
end

def add_quota(uid, cpu, memory, num_vms=nil)
    db=get_database
    quota=Quota.new(db, OpenNebula::Client.new)
    quota.set(uid.to_i, cpu.to_f, memory.to_i, num_vms)
end


result=[false, "Unknown error"]

command=ARGV.shift

case command
when "quota"
    check_parameters("quota", 1)
    
    case ARGV[0].downcase
    when 'set'
        check_parameters("quota set", 3)
        Dir.chdir VAR_LOCATION
        
        begin
            add_quota(*ARGV[1..4])
        rescue Exception => e
            puts "Error starting server: #{e}"
            exit(-1)
        end
    else
        #default
    end
    
    exit 0
    
when "login"
    check_parameters("login", 1)
    
    user=ARGV[0]
    time=ARGV[1]
    
    if time
        time=time.to_i
    else
        time=3600
    end
    
    ssh=SshAuth.new
    ssh.login(user, time)
    
when "key"
    ssh=SshAuth.new
    puts ssh.extract_public_key
    
    exit 0
    
when "help"
    print_help
    exit 0
    
when "--version"
    puts CommandParse::ONE_VERSION
    
else
    print_help
    exit -1
end

if OpenNebula.is_error?(result)
    puts "Error: " + result.message
    exit -1
end
